package com.neu.his.dms.service.impl;


import com.neu.his.common.dto.dms.DmsAcupointsParam;
import com.neu.his.dms.service.DmsAcupointsService;
import com.neu.his.mbg.mapper.DmsAcupointsMapper;
import com.neu.his.mbg.model.DmsAcupoints;
import com.neu.his.mbg.model.DmsAcupointsExample;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class DmsAcupointsServiceImpl implements DmsAcupointsService {

    @Resource
    private DmsAcupointsMapper dmsAcupointsMapper;

    @Override
    public int create(DmsAcupointsParam DmsAcupointsParam) {
        DmsAcupoints DmsAcupoints = new DmsAcupoints();
        BeanUtils.copyProperties(DmsAcupointsParam, DmsAcupoints);
        DmsAcupoints.setStatus(1);
        //查询是否有同name结算类别
        DmsAcupointsExample example = new DmsAcupointsExample();
        example.createCriteria().andAcupointsEqualTo(DmsAcupoints.getAcupoints());
        List<DmsAcupoints> lists = dmsAcupointsMapper.selectByExample(example);
        if (lists.size() > 0) {
            return 0;
        }
        //没有则插入数据
        return dmsAcupointsMapper.insert(DmsAcupoints);
    }

    @Override
    public int delete(List<Long> ids) {
        DmsAcupoints DmsAcupoints = new DmsAcupoints();
        DmsAcupoints.setStatus(0);
        DmsAcupointsExample example = new DmsAcupointsExample();
        example.createCriteria().andIdIn(ids);
        return dmsAcupointsMapper.updateByExampleSelective(DmsAcupoints, example);
    }

    @Override
    public int update(Long id, DmsAcupointsParam DmsAcupointsParam) {
        DmsAcupoints DmsAcupoints = new DmsAcupoints();
        BeanUtils.copyProperties(DmsAcupointsParam, DmsAcupoints);
        DmsAcupoints.setId(id);
        return dmsAcupointsMapper.updateByPrimaryKeySelective(DmsAcupoints);
    }

    @Override
    public List<DmsAcupointsParam> select(DmsAcupointsParam DmsAcupointsParam) {

        DmsAcupointsExample example = new DmsAcupointsExample();
        DmsAcupointsExample.Criteria criteria = example.createCriteria();
        //如果没有指明state，返回不为0的
        if (DmsAcupointsParam.getStatus() == null) {
            criteria.andStatusNotEqualTo(0);
        }
        //是否按结算类别名、比例查询
        if (!StringUtils.isEmpty(DmsAcupointsParam.getAcupoints())) {
            criteria.andAcupointsLike("%" + DmsAcupointsParam.getAcupoints() + "%");
        }

        //返回数据包装成Result
        List<DmsAcupoints> bsclist = dmsAcupointsMapper.selectByExample(example);
        List<DmsAcupointsParam> bscResults = new ArrayList<>();
        for (DmsAcupoints b : bsclist) {
            DmsAcupointsParam r = new DmsAcupointsParam();
            BeanUtils.copyProperties(b, r);
            bscResults.add(r);
        }
        return bscResults;
    }

    @Override
    public List<DmsAcupointsParam> selectAll() {
        DmsAcupointsExample example = new DmsAcupointsExample();
        example.createCriteria().andStatusNotEqualTo(0);
        //返回数据包装成Result
        List<DmsAcupoints> bsclist = dmsAcupointsMapper.selectByExample(example);
        List<DmsAcupointsParam> bscResults = new ArrayList<>();
        for (DmsAcupoints b : bsclist) {
            DmsAcupointsParam r = new DmsAcupointsParam();
            BeanUtils.copyProperties(b, r);
            bscResults.add(r);
        }
        return bscResults;
    }

}
